package com.lm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lm.dao.TEntityCourseMapper;
import com.lm.dao.TEntityCourseTypeMapper;
import com.lm.model.TEntityCourse;
import com.lm.model.TEntityCourseExample;
import com.lm.model.TEntityCourseType;
import com.lm.model.TEntityCourseTypeExample;
import com.lm.service.CourseService;
import com.lm.util.*;
import com.lm.vo.SimpleCourse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author wn
 * @Date 2019/3/31 16:22
 * @Version 1.0
 **/
@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    private TEntityCourseMapper courseMapper;
    @Autowired
    private TEntityCourseTypeMapper courseTypeMapper;

    @Override
    public Integer addEntity(TEntityCourse course) {
        String currentUserId = SecurityUitil.getCurrentUserId();
        course.setCreateby(currentUserId);
        course.setEditby(currentUserId);
        course.setId(Guid.get());
        course.setCreatetime(new Date());
        course.setEdittime(new Date());
        course.setStatue("1");
        return courseMapper.insert(course);
    }

    @Override
    public Integer updateEntity(TEntityCourse course) {
        TEntityCourse TEntityCourse = courseMapper.selectByPrimaryKey(course.getId());
        course.setEditby(SecurityUitil.getCurrentUserId());
        course.setEdittime(new Date());
        course.setStatue(TEntityCourse.getStatue());
        course.setCreateby(TEntityCourse.getCreateby());
        course.setCreatetime(TEntityCourse.getCreatetime());
        return courseMapper.updateByPrimaryKey(course);
    }

    @Override
    public Integer updateStatue(String id) {
        TEntityCourse entitycourse = courseMapper.selectByPrimaryKey(id);
        entitycourse.setStatue("0");
        return courseMapper.updateByPrimaryKey(entitycourse);
    }

    @Override
    public PageInfo getList(String keyword, Integer pageSize, Integer pageNumber) {
        TEntityCourseExample example = new TEntityCourseExample();
        TEntityCourseExample.Criteria criteria = example.createCriteria().andStatueEqualTo("1");
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%"+keyword+"%");
        }
        PageHelper.startPage(pageSize,pageNumber);
        return new PageInfo(courseMapper.selectWithOtherByExample(example));
    }

    @Override
    public List<SimpleCourse> initMain() {
        return courseMapper.initMain();
    }

    @Override
    public ExcelBaseBean getTemplate() {
        ExcelBaseBean baseBean = new ExcelBaseBean();
        baseBean.setCategory("院校信息");
        baseBean.setManager("计算机系主任");
        baseBean.setCompany("山东科技大学");
        baseBean.setSubject("院校信息表");
        baseBean.setTitle("课程信息");
        baseBean.setAnthor("wn");
        baseBean.setCommnets("无");
        baseBean.setExcelName("课程信息" + Guid.get() + ".xls");
        List<ExcelSheet> excelSheets = new ArrayList<>();
        ExcelSheet sheet = new ExcelSheet();
        sheet.setCreateSheet("课程信息表");
        List<ExcelSheetTitle> sheetTitles = new ArrayList<>();
        ExcelSheetTitle title1 = new ExcelSheetTitle(0,10 * 256,"课程名称");
        ExcelSheetTitle title2 = new ExcelSheetTitle(1,10 * 256,"英文名称");
        ExcelSheetTitle title3 = new ExcelSheetTitle(2,10 * 256,"课程代码");
        ExcelSheetTitle title4 = new ExcelSheetTitle(3,10 * 256,"课程类型(通识必修课，通识选修课··)");
        sheetTitles.add(title1);
        sheetTitles.add(title2);
        sheetTitles.add(title3);
        sheetTitles.add(title4);
        sheet.setSheetTitles(sheetTitles);
        excelSheets.add(sheet);
        baseBean.setSheets(excelSheets);
        return baseBean;
    }

    @Override
    public List<TEntityCourseType> getAllCourseType() {
        TEntityCourseTypeExample example = new TEntityCourseTypeExample();
        example.createCriteria();
        return courseTypeMapper.selectByExample(example);
    }

    @Override
    public int addEntities(List<TEntityCourse> courses) {
        int suc = 0;
        for (TEntityCourse course : courses) {
            String currentUserId = SecurityUitil.getCurrentUserId();
            course.setCreateby(currentUserId);
            course.setEditby(currentUserId);
            course.setId(Guid.get());
            course.setCreatetime(new Date());
            course.setEdittime(new Date());
            course.setStatue("1");
            suc += courseMapper.insert(course);
        }
        return suc;
    }
}
